package com.ruoyi.web.controller.equ;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.equ.domain.BrandModelChildVo;
import com.ruoyi.equ.domain.BrandModelVo;
import com.ruoyi.equ.domain.FaultType;
import com.ruoyi.equ.domain.MonthlyFaultRate;
import com.ruoyi.equ.domain.vo.SignAndStampVo;
import com.ruoyi.equ.service.IEquAssetService;
import com.ruoyi.equ.service.IEquRepairService;
import com.ruoyi.equ.service.impl.EquRepairServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/equ/process")
public class EquProcessController {
    @Autowired
    private IEquRepairService equRepairService;
    @Autowired
    private IEquAssetService equAssetService;

    /**
     * 2.1获取审批人的签字以及所属部门的电子盖章
     */
    @GetMapping("/getSignAndStamp")
    public AjaxResult getSignAndStamp() {
        try {
            SignAndStampVo result = equRepairService.getSignAndStamp();
            return AjaxResult.success(result);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("操作失败");
        }
    }

    /**
     * 2.4获取验收人签字，以及获取科室负责人签字
     */
    @GetMapping("/getSignAndLeaderSignAndStamp")
    public AjaxResult getSignAndLeaderSignAndStamp() {
        try {
            SignAndStampVo result = equRepairService.getSignAndStamp();
            return AjaxResult.success(result);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("操作失败");
        }
    }

    /**
     * 初始化 equipmentData
     */
    @GetMapping("/getEquipmentData")
    public AjaxResult getEquipmentData() {
        try {
            Map<String, Map<String, Integer>> result = equAssetService.getEquipmentData();

            return AjaxResult.success(result);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("操作失败");
        }
    }

    /**
     * 获取到所有品牌
     */
    @GetMapping("/getAllAssetBrandAndModelSpec")
    public AjaxResult getAllAssetBrandAndModelSpec() {
        try {
            List<BrandModelVo> result = equAssetService.selectAllAssetBrandAndModelSpec();
            return AjaxResult.success(result);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("操作失败");
        }
    }

    /**
     * 获取品牌以及设备的月故障率趋势统计
     */
    @GetMapping("/getMonthlyFaultRateData")
    public AjaxResult getMonthlyFaultRateData(  @RequestParam(value = "brand", required = false) String brand,
                                                @RequestParam(value = "model", required = false) String model) {
        try {
            Map<String, Object> params = new HashMap<>();
            if (brand != null) params.put("brand", brand);
            if (model != null) params.put("model", model);
            System.out.println("getMonthlyFaultRateData All params:" + params);
            List<MonthlyFaultRate> result = equAssetService.getMonthlyFaultRateData(params);
            return AjaxResult.success(result);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("操作失败");
        }
    }

    /**
     * 获取故障类型占比分析
     *   //faultTypeData: [
     *         //         { type: '部件老化', count: 45, percentage: 30 },
     *         //         { type: '操作不当', count: 30, percentage: 20 },
     *         //         { type: '软件故障', count: 25, percentage: 16.7 },
     *         //         { type: '电源问题', count: 20, percentage: 13.3 },
     *         //         { type: '其他', count: 30, percentage: 20 }
     *         //       ],
     */
    @GetMapping("/getFaultTypeData")
    public AjaxResult getFaultTypeData( @RequestParam(value = "brand", required = false) String brand,
                                        @RequestParam(value = "model", required = false) String model) {
        try {
            Map<String, Object> params = new HashMap<>();
            if (brand != null) params.put("brand", brand);
            if (model != null) params.put("model", model);

            System.out.println("getFaultTypeData All params:" + params);
            List<FaultType> result = equRepairService.getFaultTypeData(params);
            return AjaxResult.success(result);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("操作失败");
        }
    }
}
